package com.qf.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qf.entity.Goods;
import com.qf.mapper.GoodsMapper;
import com.qf.service.IGoodsService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Override
    public Map<Integer, Goods> getGoodsMap(List<Integer> gidList) {

        // 1.一个Map最终返回
        Map<Integer,Goods> goodsMap = new HashMap<>();

        // 2.设置条件
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.in("id", gidList);

        // 3.查询goods
        List<Map<String, Object>> goodsList = baseMapper.selectMaps(wrapper);

        // 4.把数据封装到goodsMap中
        for (Map<String, Object> map : goodsList) {
            Goods goods = new Goods();
            try {
                BeanUtils.populate(goods,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            goodsMap.put(goods.getId(),goods);
        }
        return goodsMap;
    }
}
